package cn.bluemobi.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.app.entity.form.CollectionsForm;
import cn.bluemobi.app.entity.output.Collections;
import cn.bluemobi.app.mapper.CollectionMapper;
import cn.bluemobi.app.service.CollectionService;
import cn.bluemobi.common.core.dto.Dto;
import cn.bluemobi.common.core.dto.ListDto;

/**
 * 
 * @author lvxh
 * 
 */
@Service
@Transactional
public class CollectionServiceImpl implements CollectionService {

	@Autowired
	private CollectionMapper collectionMapper;

	@Override
	public ListDto<Collections> findCollectionList(String memberId, String type) {
		ListDto<Collections> dto = new ListDto<>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("memberId", memberId);
		params.put("type", type);
		List<Collections> collectiones = collectionMapper
				.findCollectionList(params);
		dto.setList(collectiones);
		return dto;
	}

	@Override
	public Dto<Collections> saveOrUpdateCollection(String memberId,
			String merchantId, String commodityId, String type) {
		Dto<Collections> dto = new Dto<Collections>();
		if (!StringUtils.isEmpty(type)
				&& StringUtils.equals(type, "0")
				&& !StringUtils.isEmpty(commodityId)
				&& collectionMapper.checkIsCollectionCommodityId(memberId,
						commodityId) > 0) {
			dto.errorMsg("该商品已收藏");
			return dto;
		}
		if (!StringUtils.isEmpty(type)
				&& StringUtils.equals(type, "1")
				&& collectionMapper.checkIsCollectionMerchantId(memberId,
						merchantId) > 0) {
			dto.errorMsg("该商户已收藏");
			return dto;
		}
		String collectionId = null;
		if (!StringUtils.isEmpty(type) && StringUtils.equals(type, "0")) {
			// 新增商品收藏
			CollectionsForm collectionsCommodityForm = new CollectionsForm();
			collectionsCommodityForm.setMemberId(memberId);
			collectionsCommodityForm.setCommodityId(commodityId);
			collectionsCommodityForm.setType("0");
			collectionMapper.insertCollection(collectionsCommodityForm);
			if (collectionMapper.checkIsCollectionMerchantId(memberId,
					merchantId) <= 0) {
				// 新增商户收藏
				CollectionsForm collectionsMerchantForm = new CollectionsForm();
				collectionsMerchantForm.setMemberId(memberId);
				collectionsMerchantForm.setMerchantId(merchantId);
				collectionsMerchantForm.setType("1");
				collectionMapper.insertCollection(collectionsMerchantForm);
			}
			collectionId = collectionsCommodityForm.getId();
		} else {
			// 新增商户收藏
			CollectionsForm collectionsMerchantForm = new CollectionsForm();
			collectionsMerchantForm.setMemberId(memberId);
			collectionsMerchantForm.setMerchantId(merchantId);
			collectionsMerchantForm.setType("1");
			collectionMapper.insertCollection(collectionsMerchantForm);
			collectionId = collectionsMerchantForm.getId();
		}

		Collections collections = new Collections();
		collections.setCollectionId(collectionId);
		dto.setData(collections);
		return dto;
	}

	@Override
	public Dto<Object> deleteCollection(String memberId, String collectionId) {
		Dto<Object> dto = new Dto<Object>();
		collectionMapper.deleteCollection(collectionId);
		return dto;
	}

}
